package com.android.functionmanagement.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import com.android.functionmanagement.BaseActivity;
import com.android.functionmanagement.adapter.BaseApdater;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.util.DeviceUtils;
import com.android.functionmanagement.util.JSONUtil;
import com.android.functionmanagement.util.SharedPreferencesUtils;
import com.uwatch.helowin.R;

public class FamiliarityNumberActivity extends BaseActivity {

	private GridView familynumbers;
	private FamiliarityAdapter familiarityAdapter;
	private ArrayList<HashMap<String, Object>> datas = new ArrayList<HashMap<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.familiaritynumber_layout);
		getFamilyNumbers();
		setupViews();

		familiarityAdapter = new FamiliarityAdapter(datas, this);
		familynumbers.setAdapter(familiarityAdapter);
		familynumbers.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				if(position == familynumbers.getCount() - 1 || familynumbers.getCount() == 1 ){
					//GOTO DIAL
					Intent intent = new Intent(Intent.ACTION_DIAL);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}else{
					//DIAL
					Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + datas.get(position).get(ConstantGloble.MOBILE)));
					startActivity(intentPhone);
				}
			}
		});
	}
	
	@Override
	protected void updateDisplayData() {
		super.updateDisplayData();
		getFamilyNumbers();
		familiarityAdapter.notifyDataSetChanged(datas);
	}

	private void setupViews() {
		familynumbers = (GridView) findViewById(R.id.familynumbers);
	}

	// PhoneName1，PhoneNo1，PhoneName2，PhoneNo2，PhoneName3，PhoneNo3。
	private void getFamilyNumbers() {
		HashMap<String, Object> map;
		String dataStrings = SharedPreferencesUtils.getParam(this,
				"numberList", "").toString();
		dataStrings = dataStrings.replace("}, {", "},{").replace("=", "=\"")
				.replace(", ", "\", ").replace("}", "\"}")
				.replace("},{", "}, {");
		try {
			datas = (ArrayList<HashMap<String, Object>>) JSONUtil
					.parseJsonResponse(dataStrings);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		for (HashMap<String, Object> data : datas) {
			if (!DeviceUtils.haveContact(data.get(ConstantGloble.NAME)
					.toString(), this)) {
				DeviceUtils
						.insertContact(this, data.get(ConstantGloble.NAME)
								.toString(), data.get(ConstantGloble.MOBILE)
								.toString());
			}
		}
		map = new HashMap<String, Object>();
		map.put("dial", "拨号");
		datas.add(map);
	}

	public class FamiliarityAdapter extends BaseApdater {

		public FamiliarityAdapter(ArrayList list, Context mcContext) {
			super(list, mcContext);
		}
		public void notifyDataSetChanged(ArrayList list){
			this.mList = list;
			notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View arg1, ViewGroup arg2) {
			if (arg1 == null) {
				arg1 = View.inflate(mContext, R.layout.familiarity_item_layout,
						null);
			}
			HashMap<String, Object> data = (HashMap<String, Object>) mList
					.get(position);
			TextView name = (TextView) arg1.findViewById(R.id.name);
			TextView phone = (TextView) arg1.findViewById(R.id.phone);
			if (position == getCount() - 1 || getCount() == 1 ) {// LAST
				phone.setVisibility(View.GONE);
				name.setText(data.get("dial").toString());
			} else {
				phone.setVisibility(View.VISIBLE);
				name.setText(data.get(ConstantGloble.NAME).toString());
				phone.setText(data.get(ConstantGloble.MOBILE)+"");
			}
			return arg1;
		}
	}
}
